# $ANTLR 3.1 Research.g 2008-09-05 17:03:14

import sys
from antlr3 import *
from antlr3.compat import set, frozenset

from antlr3.tree import *



# for convenience in actions
HIDDEN = BaseRecognizer.HIDDEN

# token types
String=5
UnicodeEscape=7
Digit=9
HexDigit=8
EscapeSequence=6
EOF=-1
STRING=4

# token names
tokenNames = [
    "<invalid>", "<EOR>", "<DOWN>", "<UP>", 
    "STRING", "String", "EscapeSequence", "UnicodeEscape", "HexDigit", "Digit"
]




class ResearchParser(Parser):
    grammarFileName = "Research.g"
    antlr_version = version_str_to_tuple("3.1")
    antlr_version_str = "3.1"
    tokenNames = tokenNames

    def __init__(self, input, state=None):
        if state is None:
            state = RecognizerSharedState()

        Parser.__init__(self, input, state)







                
        self._adaptor = CommonTreeAdaptor()


        
    def getTreeAdaptor(self):
        return self._adaptor

    def setTreeAdaptor(self, adaptor):
        self._adaptor = adaptor

    adaptor = property(getTreeAdaptor, setTreeAdaptor)


    class prog_return(ParserRuleReturnScope):
        def __init__(self):
            ParserRuleReturnScope.__init__(self)

            self.tree = None




    # $ANTLR start "prog"
    # Research.g:12:1: prog : string ;
    def prog(self, ):

        retval = self.prog_return()
        retval.start = self.input.LT(1)

        root_0 = None

        string1 = None



        try:
            try:
                # Research.g:12:5: ( string )
                # Research.g:12:7: string
                pass 
                root_0 = self._adaptor.nil()

                self._state.following.append(self.FOLLOW_string_in_prog35)
                string1 = self.string()

                self._state.following.pop()
                self._adaptor.addChild(root_0, string1.tree)



                retval.stop = self.input.LT(-1)


                retval.tree = self._adaptor.rulePostProcessing(root_0)
                self._adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop)


            except RecognitionException, re:
                self.reportError(re)
                self.recover(self.input, re)
                retval.tree = self._adaptor.errorNode(self.input, retval.start, self.input.LT(-1), re)
        finally:

            pass

        return retval

    # $ANTLR end "prog"

    class string_return(ParserRuleReturnScope):
        def __init__(self):
            ParserRuleReturnScope.__init__(self)

            self.tree = None




    # $ANTLR start "string"
    # Research.g:14:1: string : String -> ^( STRING String ) ;
    def string(self, ):

        retval = self.string_return()
        retval.start = self.input.LT(1)

        root_0 = None

        String2 = None

        String2_tree = None
        stream_String = RewriteRuleTokenStream(self._adaptor, "token String")

        try:
            try:
                # Research.g:15:2: ( String -> ^( STRING String ) )
                # Research.g:15:4: String
                pass 
                String2=self.match(self.input, String, self.FOLLOW_String_in_string44) 
                stream_String.add(String2)

                # AST Rewrite
                # elements: String
                # token labels: 
                # rule labels: retval
                # token list labels: 
                # rule list labels: 

                retval.tree = root_0

                if retval is not None:
                    stream_retval = RewriteRuleSubtreeStream(self._adaptor, "token retval", retval.tree)
                else:
                    stream_retval = RewriteRuleSubtreeStream(self._adaptor, "token retval", None)


                root_0 = self._adaptor.nil()
                # 15:11: -> ^( STRING String )
                # Research.g:15:14: ^( STRING String )
                root_1 = self._adaptor.nil()
                root_1 = self._adaptor.becomeRoot(self._adaptor.createFromType(STRING, "STRING"), root_1)

                self._adaptor.addChild(root_1, stream_String.nextNode())

                self._adaptor.addChild(root_0, root_1)



                retval.tree = root_0



                retval.stop = self.input.LT(-1)


                retval.tree = self._adaptor.rulePostProcessing(root_0)
                self._adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop)


            except RecognitionException, re:
                self.reportError(re)
                self.recover(self.input, re)
                retval.tree = self._adaptor.errorNode(self.input, retval.start, self.input.LT(-1), re)
        finally:

            pass

        return retval

    # $ANTLR end "string"


    # Delegated rules


 

    FOLLOW_string_in_prog35 = frozenset([1])
    FOLLOW_String_in_string44 = frozenset([1])



def main(argv, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr):
    from antlr3.main import ParserMain
    main = ParserMain("ResearchLexer", ResearchParser)
    main.stdin = stdin
    main.stdout = stdout
    main.stderr = stderr
    main.execute(argv)


if __name__ == '__main__':
    main(sys.argv)
